package com.zq.englishlearnserve.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zq.englishlearnserve.common.entity.AjaxResult;
import com.zq.englishlearnserve.entity.User;
import com.zq.englishlearnserve.entity.Words;
import com.zq.englishlearnserve.service.UserService;
import com.zq.englishlearnserve.service.UserWordService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user/")
public class UserController extends BaseController{

    @Autowired
    WxMaService wxMaService;

    @Autowired
    UserService userService;

    @Autowired
    UserWordService userWordService;

    // 测试登录，浏览器访问： http://localhost:8080/user/doLogin?username=zhang&password=123456
    @PostMapping("doLogin")
    public AjaxResult doLogin(@RequestBody Map map) {
        String code = map.get("wxCode").toString();
        try {
            String openid = "";
            if (code.equals("xxxxxxxxxx")) {
                openid = code;
            } else {
                WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
                openid = session.getOpenid();
                log.info(session.getSessionKey());
                log.info(openid);
            }
            if (StringUtils.isNotBlank(openid)) {
                // 查询当前用户是否有openid，没有则新增
                userService.saveUserByWxIdIfNotExist(openid);
                // 登录哦
                StpUtil.login(openid);
                // 返回token
                SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
                // 设置session
                User loginUser = userService.getByWxId(openid);
                StpUtil.getSession().set("user", loginUser);

                return AjaxResult.success(tokenInfo);
            }
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        return AjaxResult.error("登录失败，游客模式");
    }

    @RequestMapping("getUserInfo")
    public AjaxResult getUserInfo() {
        // String loginId = StpUtil.getLoginIdAsString();
        User loginUser = (User) StpUtil.getSession().get("user");
        return AjaxResult.success(loginUser);
    }

    //获取用户计划单词，如果没有则自动分配，如未选择词书则返回空
    @PostMapping("getPlanWords")
    public AjaxResult getPlanWords(@RequestBody Map map) {
        User loginUser = getLoginUser();
        Long id = loginUser.getId();
        List<Words> wordList = userWordService.selectPlanWords(id);
    }

}
